<?php

namespace app\api\controller;

use app\common\controller\Api;
// use think\Request;
use think\Db;

header('Access-Control-Allow-Origin:*');//允许跨域


/**
 * 示例接口
 */
class Device extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['test3', 'test1','get','put'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['test2','get','put'];

    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/device/test2/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */
    public function test()

    {
        $i=file_get_contents('num.txt');
        ++$i;
        file_put_contents('num.txt',$i);
    

        $this->success('返回成功'.$i, $this->request->param());

    }

    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $this->success('返回成功', ['action' => 'test1']);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {

        $this->success('返回成功23', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

    public function get()
    {
        // $account = $this->request->request('user/s'); 
        // $password = $this->request->request('password/s');
        $data = $this->request->param();
        $account = empty($data['user'])?'':$data['user'];
        $password = empty($data['password'])?'':$data['password'];
        $deviceid = empty($data['deviceid'])?0:$data['deviceid'];
        $valtype = empty($data['valtype'])?1:$data['valtype'];
      
        // dump($this->request->param());
        // echo($account);
        // echo $password;
        // 账号密码查询
        if(empty($account)||empty($password))
            return '请传入账号user密码password';
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if (!$ret) {
            $this->error($this->auth->getError());
        } else {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $userid = $data['userinfo']['id'];
            //
            // echo $userid;
            $user = $this->get_device($userid);
            // dump($user);
            if(!$user){
                $this->success('错误消息', ['msg' => '你还没有设备,赶紧去添加吧']);
            }else{
                // echo $deviceid;
                if($deviceid<1){return '请传你的设备deviceid参数';}
                $device = $this->get_deviceid($deviceid);
                if($device){
                    switch ($valtype) {
                        case '1':
                            return $device['val'];
                            break;
                        
                        default:
                            return $device;
                            break;
                    }
                    // dump($device);
                }else{
                    return '你没有设备id为'.$deviceid;
                }
                
            }
            
            // $this->success(__('Logged in successful'), $data);

        }
        // $this->success('返回成功', ['action' => 'test3']);
    }

    public function put()
    {
        // $account = $this->request->request('user/s'); 
        // $password = $this->request->request('password/s');
        $data = $this->request->param();
        $account = empty($data['user'])?'':$data['user'];
        $password = empty($data['password'])?'':$data['password'];
        $deviceid = empty($data['deviceid'])?0:$data['deviceid'];
        $val = empty($data['val'])?1:$data['val'];
      
        // dump($this->request->param());
        // echo($account);
        // echo $password;
        // 账号密码查询
        if(empty($account)||empty($password))
            return '请传入账号user密码password';
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if (!$ret) {
            $this->error($this->auth->getError());
        } else {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $userid = $data['userinfo']['id'];
            //
            // echo $userid;
            $user = $this->get_device($userid);
            // dump($user);
            if(!$user){
                $this->success('错误消息', ['msg' => '你还没有设备,赶紧去添加吧']);
            }else{
                // echo $deviceid;
                if($deviceid<1){return '请传你的设备deviceid参数';}
                $device = $this->get_deviceid($deviceid);
                if($device){
                    if(!empty($val)){
                        $this->set_deviceval($deviceid,['val'=>$val]);
                    }
                    // dump($device);
                }else{
                    return '你没有设备id为'.$deviceid;
                }
                
            }
            
            // $this->success(__('Logged in successful'), $data);

        }
        // $this->success('返回成功', ['action' => 'test3']);
    }



     /**
     * 获取是否有设备  userid
     *
     */
    private function get_device($userid){
        $user = DB::table('he_device')->where('userid',$userid)->select();
        return $user;
    }

     /**
     * 获取设备  id
     *
     */
    private function get_deviceid($deviceid){
        $device = DB::table('he_detype')->where('pid',$deviceid)->find();
        return $device;
    }

    /**
     * 设置设备id的值
     * 
     */
    private function set_deviceval($deviceid,$data){

        $device = DB::table('he_detype')->where('pid',$deviceid)->update($data);
        return $device;
    }

}
